package com.example.gyt.controller;

import com.example.gyt.pojo.Commodity;
import com.example.gyt.pojo.PageResult;
import com.example.gyt.pojo.User;
import com.example.gyt.service.CommodityService;
import com.example.gyt.service.UserService;
import com.example.gyt.util.DateUtil;
import com.example.gyt.util.ResultUtil;
import com.example.gyt.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class CommodityJsonController {

    @Autowired
    private CommodityService commodityService;

    //添加商品方法
    @ResponseBody
    @RequestMapping(value = "/member/addingGoods",method = RequestMethod.POST)
    public ResultUtil addingGoods(@Validated @RequestBody Commodity commodity, BindingResult result){
        try {

            //是否为空检验
            if(StringUtils.isEmpty(commodity.getPublisherId())){
                return ResultUtil.failure("发布人id不能为空");
            }
            if(StringUtils.isEmpty(commodity.getProductName())){
                return ResultUtil.failure("商品名称不能为空");
            }
            if(StringUtils.isEmpty(commodity.getCommodityContact())){
                return ResultUtil.failure("商品联系人不能为空");
            }

            commodity.setId(UuidUtil.getUUID());

            //获取到当前时间
            commodity.setCreateTime(DateUtil.getCurrentTime());

            int count = commodityService.insertSelective(commodity);
            if (count == 0){
                return ResultUtil.success("添加商品失败");
            }

            return ResultUtil.success("添加商品信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }

    }

    //删除一件商品
    @ResponseBody
    @RequestMapping(value = "/member/deleteCommodity",method = RequestMethod.POST)
    public ResultUtil deleteCommodity(@Validated @RequestBody Commodity commodity, BindingResult result){
        try {

            if(StringUtils.isEmpty(commodity.getId())){
                return ResultUtil.failure("商品ID不能为空");
            }

            int count = commodityService.deleteByPrimaryKey(commodity.getId());
            if (count == 0){
                return ResultUtil.failure("删除商品失败");
            }

            return ResultUtil.success("删除商品成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //商品ID查信息
    @ResponseBody
    @RequestMapping(value = "/member/queryCommodity",method = RequestMethod.POST)
    public ResultUtil queryCommodity(@Validated @RequestBody Commodity commodity, BindingResult result){
        try {

            if(StringUtils.isEmpty(commodity.getId())){
                return ResultUtil.failure("商品ID不能为空");
            }

            Commodity commodity1 = commodityService.selectByPrimaryKey(commodity.getId());
            if (commodity1 == null){
                return ResultUtil.failure("获取商品信息失败");
            }

            return ResultUtil.success(commodity1);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //修改商品信息
    @ResponseBody
    @RequestMapping(value = "/member/updateCommodity",method = RequestMethod.POST)
    public ResultUtil updateCommodity(@Validated @RequestBody Commodity commodity, BindingResult result){
        try {
            if(StringUtils.isEmpty(commodity.getId())){
                return ResultUtil.failure("商品ID不能为空");
            }

            /*if(StringUtils.isEmpty(user.getUserName())){
                user.setUserName(null);
            }

            if(StringUtils.isEmpty(user.getUserBrithday())){
                user.setUserBrithday(null);
            }

            if(StringUtils.isEmpty(user.getUserAddress())){
                user.setUserAddress(null);
            }

            if(StringUtils.isEmpty(user.getUserTel())){
                user.setUserTel(null);
            }*/

            //更新修改时间
            commodity.setUpdateTime(DateUtil.getCurrentTime());

            int count = commodityService.updateByPrimaryKeySelective(commodity);
            if (count == 0){
                return ResultUtil.failure("修改商品信息失败");
            }

            return ResultUtil.success("修改商品信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //集合于商品分页查询，所有商品查询，商品类型查询，商品名字模糊查询
    @ResponseBody
    @RequestMapping(value = "/member/allCommodityList",method = RequestMethod.POST)
    public ResultUtil allCommodityList(@Validated @RequestBody Commodity commodity, BindingResult result){
        try {

            List<Commodity> commodity1 = commodityService.getAllCommodityList(commodity);
            if (commodity1.size() == 0){
                return ResultUtil.failure("查询失败,无该数据");
            }

            //查询数据有多少条
            int countLike = commodityService.queryCountLike(commodity);

            //加载实体将数据返回给用户
            PageResult pageResult = new PageResult();
            if (commodity.getPageResult() != null){
                pageResult.setCurrentPage(commodity.getPageResult().getCurrentPage());
                pageResult.setPageSize(commodity.getPageResult().getPageSize());

            }

            pageResult.setTotalSize(countLike);
            pageResult.setItems(commodity1);

            return ResultUtil.success(pageResult);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }
}
